<template>
  <div id="tags-view-container" class="tags-view-container">
    <scroll-pane ref="scrollPane" class="tags-view-wrapper" @scroll="handleScroll">
      <Syx-Tag :type="xsyxs(i)" @contextmenu.prevent.native="openMenu(v,i,$event)" ref="tag" @click="tagCli(v,i)"
               @close="tagClose(v,i)"
               :clear="!v.clear" v-for="(v,i) in itemsListTag" :key="i">{{ v.menuname }}
      </Syx-Tag>
    </scroll-pane>
    <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
      <li @click="pageCli(1)"><i class="el-icon-refresh-right"></i> 刷新页面</li>
      <li v-if="isAffix()" @click="pageCli(2)"><i class="el-icon-close"></i> 关闭当前</li>
      <li @click="pageCli(3)"><i class="el-icon-circle-close"></i> 关闭其他</li>
      <li v-if="isFirstView()" @click="pageCli(4)"><i class="el-icon-back"></i> 关闭左侧</li>
      <li v-if="isLastView()" @click="pageCli(5)"><i class="el-icon-right"></i> 关闭右侧</li>
      <li @click="pageCli(6)"><i class="el-icon-circle-close"></i> 全部关闭</li>
    </ul>
  </div>
</template>

<script>
import ScrollPane from './ScrollPane'

export default {
  components: {ScrollPane},
  data() {
    return {
      itemsListTag: this.nativeplacestore.get("routerTags"),
      da: this.nativeplacestore.get("defaultActive"),
      visible: false,
      top: 0,
      left: 0,
      yxs: null,// 当前右键显示
      xs: 0, // 当前显示
    }
  },
  created() {
    this.xs = this.itemsListTag.findIndex((v) => v.path == this.da)
  },
  computed: {},
  watch: {
    visible(value) {
      if (value) {
        document.body.addEventListener('click', this.closeMenu)
      } else {
        document.body.removeEventListener('click', this.closeMenu)
      }
    },
    $route() {
      this.itemsListTag = this.nativeplacestore.get("routerTags")
      this.da = this.nativeplacestore.get("defaultActive")
      this.xs = this.itemsListTag.findIndex((v) => v.path == this.da)
    }
  },
  mounted() {
  },
  methods: {
    tagClose(v, i) {
      // if (this.xs == i) {
      //   this.nativeplacestore.set("defaultActive", this.itemsListTag[this.xs - 1]['path'])
      // }
      // this.itemsListTag.splice(i, 1);
      // this.xs = this.itemsListTag.findIndex((v) => v.path == this.nativeplacestore.set("defaultActive"))
      // this.nativeplacestore.set("routerTags", this.itemsListTag)
      // this.$router.push(this.itemsListTag[this.xs - 1]['path']);
    },
    isAffix() {
      if (!this.visible && !this.yxs) return false;
      return !this.itemsListTag[this.yxs]['clear'];
    },
    isFirstView() {
      if (!this.visible && !this.yxs) return false;
      return this.yxs > 1;
    },
    isLastView() {
      if (!this.visible && !this.yxs) return false;
      return this.yxs < this.itemsListTag.length - 1;
    },
    pageCli(i) {
      switch (i) {
        case 1:  // 刷新页面
          this.$router.go(0)
          break;
        case 2:  // 关闭当前
          // if (this.xs <= this.yxs) {
          //   this.nativeplacestore.set("defaultActive", this.itemsListTag[this.xs - 1]['path'])
          // }else{
          //
          // }
          // this.itemsListTag.splice(this.yxs, 1);
          // this.xs = this.itemsListTag.findIndex((v) => v.path == this.nativeplacestore.set("defaultActive"))
          // this.nativeplacestore.set("routerTags", this.itemsListTag)
          break;
        case 3:  // 关闭其他
          if (this.yxs >= 1) {
            this.itemsListTag = [{menuname: "首页", path: '/', clear: true}, this.itemsListTag[this.yxs]]
          } else {
            this.itemsListTag = [{menuname: "首页", path: '/', clear: true}]
          }
          this.nativeplacestore.set("routerTags", this.itemsListTag)
          break;
        case 4:  // 关闭左侧
          let arr = this.itemsListTag.slice(this.yxs, this.itemsListTag.length)
          this.itemsListTag = [{menuname: "首页", path: '/', clear: true}, ...arr]
          this.nativeplacestore.set("routerTags", this.itemsListTag)
          break;
        case 5:  // 关闭右侧
          let arrRight = this.itemsListTag.slice(0, this.yxs + 1)
          this.itemsListTag = [...arrRight]
          this.nativeplacestore.set("routerTags", this.itemsListTag)
          break;
        case 6:  // 全部关闭
          this.itemsListTag = [{menuname: "首页", path: '/', clear: true}]
          this.nativeplacestore.set("defaultActive", '/')
          this.nativeplacestore.set("routerTags", this.itemsListTag)
          this.$router.push('/');
          break;
        default:
          this.$message({
            showClose: true,
            message: "错误标识",
            type: "warning",
          });
          break;
      }
    },
    xsyxs(i) {
      if (this.yxs == i) {
        return 'warning';
      }
      if (this.xs == i) {
        return 'success';
      }
      return 'primary';
    },
    tagCli(v, i) {
      this.xs = i;
      this.moveToCurrentTag(i)
      this.nativeplacestore.set("defaultActive", v.path)
      this.$router.push(v.path);
    },
    moveToCurrentTag(i) {
      this.$nextTick(() => {
        const tags = this.$refs.tag
        this.$refs.scrollPane.moveToTarget(tags[i])
      })
    },
    openMenu(tag, i, e) {
      this.yxs = i;
      const menuMinWidth = 105
      const offsetLeft = this.$el.getBoundingClientRect().left
      const offsetWidth = this.$el.offsetWidth
      const maxLeft = offsetWidth - menuMinWidth

      const left = e.clientX - offsetLeft + 15

      if (left > maxLeft) {
        this.left = maxLeft
      } else {
        this.left = left
      }

      this.top = this.$el.offsetHeight
      this.visible = true
    },
    closeMenu() {
      this.yxs = null;
      this.visible = false
    },
    handleScroll() {
      this.closeMenu()
    }
  }
}
</script>

<style lang="scss" scoped>
.tags-view-container {
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
  margin-bottom: 12px;
  position: relative;

  .contextmenu {
    margin: 0;
    background: #fff;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);

    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;

      &:hover {
        background: #eee;
      }
    }
  }
}
</style>
